package cc.eddic.pratice.programmingparadigms.exam.impl;

import cc.eddic.pratice.programmingparadigms.exam.QuestionBankService;
import cc.eddic.pratice.programmingparadigms.exam.QuestionType;
import cc.eddic.pratice.programmingparadigms.exam.domain.Option;
import cc.eddic.pratice.programmingparadigms.exam.domain.Question;
import lombok.extern.slf4j.Slf4j;

import java.util.*;
import java.util.function.Predicate;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Slf4j
public class QuestionBankServiceGcsImpl extends QuestionBankServiceZyzImpl implements QuestionBankService {
    @Override
    public List<Question> importQuestions(String questionTextFile) {
        String fileContent = getQuestionFileContent();

        List<Question> questionList = new ArrayList<>();
        Question q = null;
        for (String line : fileContent.split("\n")) {
            Matcher m = questionPattern.matcher(line);
            if (m.matches()) {
                if (log.isDebugEnabled()) log.debug(line);
                String no = m.group("no");
                String desc = m.group("desc");
                if (log.isDebugEnabled()) log.debug("\t{} => {}", no, desc);
                q = new Question(Integer.parseInt(no), desc, QuestionType.SINGLE_CHOICE);
                questionList.add(q);
                continue;
            }

            m = optionPattern.matcher(line);
            if (m.matches()) {
                assert q != null;
                q.add(new Option(m.group("answer"), m.group("choice").charAt(0), false));
                continue;
            }
            if (log.isDebugEnabled()) log.debug("skip line: {}", line);
        }

        return questionList;
    }

    private final Pattern questionPattern = Pattern.compile("(?<no>[0-9]+)、(?<desc>.*)");
    private final Pattern optionPattern = Pattern.compile("(?<choice>[A-Za-z])、(?<answer>.*)");

    @Override
    public List<Question> importCorrectAnswers(String correctAnswerTextFile) {
        List<Question> questionList = importQuestions("");
        Map<Integer, Character> answerMap = readCorrectAnswers2("");

        for (Question q : questionList) {
            if (!answerMap.containsKey(q.getNo())) {
                continue;
            }

            char choice = answerMap.get(q.getNo());
            Optional<Option> optionalOption = q.getOptions().stream().filter(new Predicate<Option>() {
                @Override
                public boolean test(Option option) {
                    return option.getChoice() == choice;
                }
            }).findFirst();

            Option option2 = q.getOptions().stream()
                    .filter(option -> option.getChoice() == choice)
                    .findFirst()
                    .orElseThrow(() -> new IllegalArgumentException("question " + q.getNo() + " has no choice " + choice));


            if (optionalOption.isPresent()) {
                optionalOption.get().changeToCorrectAnswer();
            } else {
                log.error("question {} has no choice {}", q.getNo(), choice);
            }
        }
        return questionList;
    }

    @Override
    public Map<Integer, Character> readCorrectAnswers2(String correctAnswerTextFile) {
        Map<Integer, Character> result = new HashMap<>();
        String[] lines = getAnswerFileContent().split("\n");
        for (int i = 0; i < lines.length; i += 2) {
            String[] nos = lines[i].split("\t");
            String[] choices = lines[i + 1].split("\t");
            for (int j = 0; j < nos.length; j++) {
                String no = nos[j];
                String choice = choices[j];
                result.put(Integer.parseInt(no), choice.charAt(0));
            }
        }
        return result;
    }

}
